import multiprocessing
import os, time
import re
import subprocess as sp

# IDM = r'D:\Program Files (x86)\Internet Download Manager Backup\IDMan.exe'
IDM = r'D:\Program Files (x86)\Internet Download Manager\IDMan.exe'

def test_run_cmd_async():
    ie_path = "D:\\Program Files\\Internet Explorer\\iexplore.exe"
    bank_request_url = "https://corporatebank.cib.com.cn/firm/main/login/login"
    # ie_path = self.config_data["ie_path"]
    # bank_request_url = self.bank_request_url
    # os.system(f'"{ie_path}" {bank_request_url}')
    sp.Popen(f'"{ie_path}" {bank_request_url}', shell=True, stdout=sp.PIPE, stderr=sp.PIPE)

def run_cmd_async(cmd:str):
    sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
    time.sleep(0.1)

def run_cmd(cmd:str):
    # sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
    os.system(add_q(cmd))
    time.sleep(0.1)

def sep_file_name(file_name):
    res1 = re.split(r'[/\\]', file_name)
    # res1 = file_name.split('/')
    res2 = '/'.join(res1[:-1])
    return (res2, res1[-1])
    
def add_q(a):
    return r'"' + a + r'"'
    
def IDMdownload_start():
    """
    '""D:\\Program Files (x86)\\Internet Download Manager Backup\\IDMan.exe" /s"'
    or '"D:\\Program Files (x86)\\Internet Download Manager Backup\\IDMan.exe" /s'
    """
    command = r'"' + r' '.join([add_q(IDM), '/s']) + r'"'
    command = r' '.join([add_q(IDM), '/s'])
    run_cmd(command)
    
def IDMdownload(DownUrl, DownPath, file_name=None):
    if not file_name:
        DownPath, file_name = sep_file_name(DownPath)
    if os.path.exists(DownPath + "/" + file_name):
        print("{}跳过{}".format((DownPath, file_name), DownUrl))
        return
    # os.chdir(IDMPath)
    # IDM = "IDMan.exe"
    # command = r'""' + r'" "'.join([IDM, '/d', DownUrl, '/p', DownPath, '/f', FileName, '/n', '/a']) + r'""'
    command = r'"' + r' '.join([add_q(IDM), '/d', add_q(DownUrl), '/p', add_q(DownPath), '/f', add_q(file_name), '/n', '/s', '/q']) + r'"'
    command = r'"' + r' '.join([add_q(IDM), '/d', add_q(DownUrl), '/p', add_q(DownPath), '/f', add_q(file_name), '/n', '/a']) + r'"'
    command = r' '.join([add_q(IDM), '/d', add_q(DownUrl), '/p', add_q(DownPath), '/f', add_q(file_name), '/n', '/a'])
    # print(command, flush=True)
    run_cmd(command)
    

def test_idm_download():
    info = """
        https://blog.csdn.net/weixin_44072750/article/details/120707399
        使用：
        idman /s
        或
        idman /d URL [/p local_path] [/f local_file_name] [/q] [/h][/n] [/a]
        可以进行下载
        参数解释：
        /d URL  #根据URL下载文件
        /s      #开始下载队列中的任务
        /p      #定义文件要存储在本地的地址
        /f      #定义文件存储在本地的文件名
        /q      #下载成功后IDM将退出。
        /h      #下载成功后IDM将挂起你的链接
        /n      #当IDM不出问题时启动静默模式
        /a      #添加指定文件到/d的下载队列，但是不进行下载
    """
    url = "https://www.baidu.com/img/pcd_e7aeae7e7e413139a48047d2ebcd1ce9.gif"
    # print(sep_file_name(r"E:\game_python/a/b/6.gif"))
    # IDMdownload(url,r"E:\game_python/a",r"6.gif")
    # run_cmd(IDM)
    # IDMdownload(url,r"E:\a\6.gif")
    # IDMdownload(url,r"E:\a\5.gif")

    ppool = multiprocessing.Pool(processes=2)
    ppool.apply_async(IDMdownload, (url, r"E:\a\6.gif"))
    ppool.apply_async(IDMdownload, (url, r"E:\a\5.gif"))
    time.sleep(2)
    IDMdownload_start()
    ppool.close()
    ppool.join()
    print(os.getcwd())
